package com.lkj;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;

public class SomeSocketClientHandler extends SimpleChannelInboundHandler<String> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String s) throws Exception {

        System.out.println(ctx.channel().remoteAddress()+", "+s);
//        ctx.channel().writeAndFlush("from client:"+ LocalDateTime.now());
//        TimeUnit.MILLISECONDS.sleep(500);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
//        ctx.channel().writeAndFlush("from client: begin talking");
//        ctx.writeAndFlush("from client: begin talking");
//        ctx.writeAndFlush("from client: begin talking");

        for (int i = 0; i < 5; i++) {

            ctx.channel().writeAndFlush("from client: begin talking"+i+System.getProperty("line.separator"));
        }

//        ctx.channel().writeAndFlush("from client: begin talking");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
